#
#由于Trigger无法自动完成
#暂时通过手工将foodtrust.erp_inventory_warehouse_shipment表中ID大于181503(切换到正式运行)的发货记录同步到psi库
#
#需要在.bahs_profile设置环境变量
#export PSI_PASSWORD=xxxxxxx
#export RDS_AGENT=yyyy
#
#手工在命令行运行 ruby shipSomeGoods.rb
#
require 'mysql2'

    rds = Mysql2::Client.new(:host => ENV['RDS_AGENT'], :username => "psi_root", :port => '1401', :password => ENV['PSI_PASSWORD'])

    sql1 = 'select * from foodtrust.erp_inventory_warehouse_shipment where warehouse_shipment_id > 181503 and sync_to_psi_time is null;'
    #sql1 = 'select * from foodtrust.erp_inventory_warehouse_shipment where warehouse_shipment_id >= 182359;'
    res1 = rds.query(sql1)
    res1.each do |tgr|

        wss_id = tgr['warehouse_shipment_id']
        # procedure cannot be called in query method
        sqli = "call psi.shipGoodsII(#{wss_id},'#{Time.now.strftime('%Y%m%d')}')"
        puts sqli
        rdsi = Mysql2::Client.new(:host => ENV['RDS_AGENT'], :username => "psi_root", :port => '1401', :password => ENV['PSI_PASSWORD'])
        ship = rdsi.prepare(sqli)
        ship.execute();

        sync_time = Time.now()
        sqlu = "update foodtrust.erp_inventory_warehouse_shipment set sync_to_psi_time = '#{sync_time}' where warehouse_shipment_id = #{wss_id} ;"
	puts sqlu
        resu = rds.query(sqlu)
    end

    puts "Done."

rds.close
